package com.hangzhou.welbeing.welbeinginstrument.base;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 杭州惟君医疗科技有限公司16
 * Created by 20433 on 2018/1/29
 */

public class BaseRecyclerViewAdapter <T extends RecyclerView.Adapter>
        extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
    private static final int FOOTER_VIEW_TYPE = -2000;
    private static final int HEADER_VIEW_TYPE = -1000;
    private final T mBase;
    private final List<View> mFooters = new ArrayList();
    private final List<View> mHeaders = new ArrayList();
    private int position;

    public BaseRecyclerViewAdapter(T paramT)
    {
        this.mBase = paramT;
    }

    private boolean isFooter(int paramInt)
    {
        return (paramInt >= -2000) && (paramInt < this.mFooters.size() - 2000);
    }

    private boolean isHeader(int paramInt)
    {
        return (paramInt >= -1000) && (paramInt < this.mHeaders.size() - 1000);
    }

    public void addFooter(@NonNull View paramView)
    {
        if (paramView == null) {
            throw new IllegalArgumentException("You can't have a null recyclerview_footer!");
        }
        this.mFooters.add(paramView);
    }

    public void addHeader(@NonNull View paramView)
    {
        if (paramView == null) {
            throw new IllegalArgumentException("You can't have a null recyclerview_header!");
        }
        this.mHeaders.add(paramView);
    }

    public View getFooter(int paramInt)
    {
        if (paramInt < this.mFooters.size()) {
            return (View)this.mFooters.get(paramInt);
        }
        return null;
    }

    public int getFooterCount()
    {
        return this.mFooters.size();
    }

    public View getHeader(int paramInt)
    {
        if (paramInt < this.mHeaders.size()) {
            return (View)this.mHeaders.get(paramInt);
        }
        return null;
    }

    public int getHeaderCount()
    {
        return this.mHeaders.size();
    }

    public int getItemCount()
    {
        return this.mHeaders.size() + this.mBase.getItemCount() + this.mFooters.size();
    }

    public int getItemViewType(int paramInt)
    {
        if (paramInt < this.mHeaders.size()) {
            return paramInt - 1000;
        }
        if (paramInt < this.mHeaders.size() + this.mBase.getItemCount()) {
            return this.mBase.getItemViewType(paramInt - this.mHeaders.size());
        }
        return paramInt - 2000 - this.mHeaders.size() - this.mBase.getItemCount();
    }

    public T getWrappedAdapter()
    {
        return this.mBase;
    }

    public void onBindViewHolder(RecyclerView.ViewHolder paramViewHolder, int paramInt)
    {
        if (paramInt < this.mHeaders.size())
        {
            this.position = 0;
            return;
        }
        if (paramInt < this.mHeaders.size() + this.mBase.getItemCount())
        {
            this.mBase.onBindViewHolder(paramViewHolder, paramInt - this.mHeaders.size());
            this.position = (paramInt - this.mHeaders.size());
            return;
        }
        this.position = (this.mBase.getItemCount() - 1);
    }

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup paramViewGroup, int paramInt)
    {
        if (isHeader(paramInt))
        {
            paramInt = Math.abs(paramInt + 1000);
            new RecyclerView.ViewHolder((View)this.mHeaders.get(paramInt)) {};
        }
        if (isFooter(paramInt))
        {
            paramInt = Math.abs(paramInt + 2000);
            new RecyclerView.ViewHolder((View)this.mFooters.get(paramInt)) {};
        }
        return this.mBase.onCreateViewHolder(paramViewGroup, paramInt);
    }

    public void setFooterVisibility(boolean paramBoolean)
    {
        Iterator localIterator = this.mFooters.iterator();
        if (localIterator.hasNext())
        {
            View localView = (View)localIterator.next();
            if (paramBoolean) {}
            for (int i = 0;; i = 8)
            {
                localView.setVisibility(View.VISIBLE);
                break;
            }
        }
    }

    public void setHeaderVisibility(boolean paramBoolean)
    {
        Iterator localIterator = this.mHeaders.iterator();
        if (localIterator.hasNext())
        {
            View localView = (View)localIterator.next();
            if (paramBoolean) {}
            for (int i = 0;i< 8;i++)
            {
                localView.setVisibility(View.VISIBLE);
                break;
            }
        }
    }
}

